package com.libra.assemble.entity.authority;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.libra.assemble.entity.IdEntity;
import com.libra.assemble.xutil.enums.Status;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 用户信息(登录账号)管理.
 *
 * @author Libra
 */
@Entity
@Table(name = "tb_user")
public class User extends IdEntity {

    /**
     * 登录账号
     */
    private String loginName;

    /**
     * 用户名称
     */
    private String name;

    /**
     * 邮箱地址
     */
    private String email;

    /**
     * 简单密码：用户输入的密码，未经转码.
     */
    private String plainPassword;

    /**
     * 密码：经过加密的密码
     */
    private String password;

    /**
     * 密码解码的盐值
     */
    private String salt;

    /**
     * 照片
     */
    private String photo;

    /**
     * 注册日期
     */
    private Date registerDate;

    /**
     * 状态.
     */
    private Status status;

    /**
     * 用户对应的角色列表
     */
    private List<Role> roles;

    public User() {
    }

    public User(Long id) {
        this.id = id;
    }

    @NotBlank
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @NotBlank
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Email
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * 不持久化到数据库，也不在Json中关联显示的属性.
     */
    @Transient
    @JsonIgnore
    public String getPlainPassword() {
        return plainPassword;
    }

    public void setPlainPassword(String plainPassword) {
        this.plainPassword = plainPassword;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    // 设定JSON序列化时的日期格式
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @Enumerated(EnumType.ORDINAL)
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @JsonIgnore //不转化为json格式.
    @ManyToMany
    @JoinTable(
            name = "tb_user_role",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
    )
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }


}